package com.cjw.campus.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.system.entity.SubjectTable;
import com.cjw.campus.system.service.ISubjectTableService;
import com.cjw.campus.system.vo.SubjectTableVO;
import core.result.JsonResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 学科表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2022-12-04
 */
@RestController
@RequestMapping("/system/subjectTable")
@Api(tags = "学科管理", value = "学科管理接口")
public class SubjectTableController {

    @Autowired
    private ISubjectTableService subjectTableService;

    @GetMapping("/listPageBySubjectNameAndSchoolName")
    @ApiOperation(value = "模糊分页查询学科信息", notes = "模糊分页查询学科信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2"),
            @ApiImplicitParam(name = "subjectName", value = "学科名称", dataType = "String"),
            @ApiImplicitParam(name = "schoolName", value = "学校名称", dataType = "String"),
    })
    public JsonResult listPageBySubjectNameAndSchoolName (Integer currentPage,Integer pageSize,String subjectName, String schoolName) {
        IPage<SubjectTableVO> page = new Page<>(currentPage, pageSize);
        IPage<SubjectTableVO> mapPage = subjectTableService.listPageBySubjectNameAndSchoolName(page, subjectName, schoolName);
        return JsonResult.ok(mapPage);
    }

    @GetMapping("/listBySubjectId")
    @ApiOperation(value = "根据id查询学科信息", notes = "根据id查询学科信息", httpMethod = "GET")
    @ApiParam(name = "subjectId", value = "学科ID", required = true)
    public JsonResult listBySubjectId (Long subjectId) {
        SubjectTableVO listBySubjectId = subjectTableService.listBySubjectId(subjectId);
        return JsonResult.ok(listBySubjectId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有学科信息", notes = "查询所有学科信息", httpMethod = "GET")
    public JsonResult list () {
        List<SubjectTable> list = subjectTableService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/Save")
    @ApiOperation(value = "保存学科信息", notes = "保存学科信息", httpMethod = "POST")
    @ApiParam(name = "subjectTable", value = "学科实体类", required = true)
    public JsonResult save (SubjectTable subjectTable) {
        // 保存年级信息
        boolean save = subjectTableService.save(subjectTable);
        // 判断年级是否新增成功
        if (save) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新学科信息", notes = "根据id更新学科信息", httpMethod = "POST")
    @ApiParam(name = "subjectTable", value = "年级实体类", required = true)
    public JsonResult updateById (SubjectTable subjectTable) {
        boolean updateById = subjectTableService.updateById(subjectTable);
        // 判断用户是否修改成功
        if (updateById) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("修改失败");
    }

    @GetMapping("/removeById")
    @ApiOperation(value = "根据id删除学科信息", notes = "根据id删除学科信息", httpMethod = "GET")
    @ApiParam(name = "subjectId", value = "学科ID", required = true)
    public JsonResult removeById (Long subjectId) {
        boolean removeId = subjectTableService.removeById(subjectId);
//         判断用户是否删除成功
        if (removeId) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping ("/removeByIds")
    @ApiOperation(value = "根据id批量删除学科信息", notes = "根据id批量删除学科信息", httpMethod = "POST")
    @ApiParam(name = "subjectId", value = "学科ID", required = true)
    public JsonResult removeByIds (Long[] idList) {
        boolean removeSchoolIds = subjectTableService.removeByIds(Arrays.asList(idList));
        if (removeSchoolIds) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

}
